<!--
代码模板说明：
1.codeGenaratorExample 为例子用的实体名称，可批量替换掉
2.普通查询、高级查询、列表显示字段替换成自己业务的列表字段
3.请求地址自行替换
-->
<template>
  <el-container id="lap-app" v-loading="pBaseLoading">
    <el-main>
      <lap-find-bar>
        <template slot="default">
          <!-------------------------------------------------->
          <!--普通查询开始-->
          <el-input placeholder="请输入表名称" v-model="pSearchValue" style="width:400px"></el-input>
          <!--普通查询结尾-->
        </template>
      </lap-find-bar>
      <lap-table-bar>
        <template slot="title">
          触发器列表
          <span
            class="label-warning"
          >（以下展示的疑似为监控作用的触发器；本新增功能全系统禁止超过20个；本系统无法对触发器内容做任何更改；只能删除自动生成的触发器）</span>
        </template>
        <template slot="menu">
          <el-button type="primary" @click="onCreate">新增</el-button>
        </template>
      </lap-table-bar>
      <el-table
        ref="table"
        :data="pTableData.filter(item=>item.TableName.toLowerCase().indexOf((pSearchValue||'').toLowerCase())>=0)"
        :highlight-current-row="true"
      >
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column
          property="TriggerName"
          label="触发器名称"
          width="400"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <el-link @click="()=>onDetail(scope.row)">{{scope.row.TriggerName}}</el-link>
          </template>
        </el-table-column>
        <el-table-column property="TableName" label="表名" width="250" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column
          property="TriggerType"
          label="类型"
          width="150"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          property="CreatedDate"
          label="创建时间"
          width="150"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column property="Remarks" label="备注" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column
          property="IsDisabled"
          label="禁用/启用"
          width="100"
          fixed="right"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.IsDisabled"
              active-color="#00A854"
              inactive-color="#F04134"
              :active-value="0"
              :inactive-value="1"
              @change="onChangeSwitch(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" class-name="operate" fixed="right" width="60">
          <template slot-scope="scope">
            <el-popconfirm title="确定删除当前记录？" @onConfirm="()=>onDelete(scope.row)">
              <el-link slot="reference">删除</el-link>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <!--编辑页面-->
      <el-dialog
        custom-class="lap-form-edit"
        title="编辑表单"
        v-if="pEditVisible"
        :visible.sync="pEditVisible"
        :close-on-click-modal="false"
        :before-close="platBeforeCloseForm"
      >
        <TriggerEdit ref="formEdit" :detailData="selectedRow" @after-submit="onAfterSubmit"></TriggerEdit>
      </el-dialog>
    </el-main>
  </el-container>
</template>
<script>
/**
 * 此注释段用于标记页面可被自动注册到路由
 * auto-route
 */
const TriggerEdit = () => import("./TriggerEdit"); //编辑页面组件
export default {
  mixins: [$lap.BaseView],
  name: "lap-app",
  components: { TriggerEdit }, //引用组件
  data() {
    return {
      pTitle: "触发器管理", //页面标题
      pPageFunctionCode: "", //页面功能定义，例如：00100104
      selectedRow: null,
    };
  },
  methods: {
    /**
     * 平台加载方法
     */
    platLoadData() {
      const that = this;
      $lap
        .post(
          "/MLogXWeb/LogHandler.ashx",
          {
            type: "GetAllTrigger",
          },
          null,
          {
            that,
          }
        )
        .then((data) => {
          that.pTableData = data;
        });
    },
    /**
     * 搜索
     */
    onSearch(e, advanced) {
      if (advanced === true) {
        //高级查询
        this.pFilterValue = "";
        this.platBuildConditionList();
      } else {
        //普通查询
        this.platClearConditionList();
        this.pFilterValue = this.pSearchValue;
      }

      this.platLoadData();
    },
    /**
     * 新增
     */
    onCreate() {
      const that = this;
      that.selectedRow = null;
      that.pEditVisible = true;
    },
    /**
     * 编辑
     */
    onDetail(row) {
      const that = this;
      that.selectedRow = row;
      that.pEditVisible = true;
    },
    /**
     * 删除
     */
    onDelete(row) {
      const that = this;
      $lap
        .post(
          "/MLogXWeb/LogHandler.ashx",
          {
            type: "DelTrigger",
            trigger: Base64.encode(JSON.stringify(row)),
          },
          null,
          {
            that,
          }
        )
        .then(function (data) {
          that.platLoadData();
        });
    },
    /**
     * 开关触发器
     */
    onChangeSwitch(row) {
      const that = this;
      $lap.post(
        "/MLogXWeb/LogHandler.ashx",
        {
          type: "SetTriggerStatus",
          name: Base64.encode(row.TriggerName),
          tablename: Base64.encode(row.TableName),
          isOpen: row.IsDisabled === 0,
        },
        null,
        {
          that,
        }
      );
    },
    /**
     * 表单数据提交成功后
     */
    onAfterSubmit(data, opts = {}) {
      const that = this;
      if (opts.close) {
        //关闭表单
        that.pEditVisible = false;
      }
      //刷本页面处理
      that.platLoadData();
    },
  },
  watch: {},
};
</script>